$(function () {
    $('#sc').html(setVC({ bool: false })).click(function () { $('#sc').html(setVC({ bool: false })) })
    $('[name="userName"]').change(function () {
        selectName();
    })

    $('button').click(function () {
        let name = $('[name="userName"]').val();
        let pwd1 = $('[name="userPwd1"]').val();
        let pwd2 = $('[name="userPwd2"]').val();
        let vc1 = $('#ac').val();
        let vc2 = $('#sc').html()

        // if( pwd1 !== pwd2 ){
        //   $('[name="pwdSpan"]').html( '<span style="color:red">!&ensp;您输入的两次密码不同</span>' )

        //   $('#sc').html( setVC( { bool:false } ) )

        //   return ;

        // }else{
        //   $('[name="pwdSpan"]').html('');
        // }


        // if( vc1.toLowerCase() !== vc2.toLowerCase() ){
        //   $('[name="vcSpan"]').html('<span style="color:red">!&ensp;您输入的验证码有误</span>');

        //   $('#sc').html( setVC( { bool:false } ) );          // return 终止 之后程序的执行
        //   return

        // }else{
        //   $('[name="vcSpan"]').html('');

        // }

        register();

    })
})


async function selectName() {
    let res = await myPromiseAjax({
        url: '../server/goods_select_name.php',
        type: 'post',
        data: { userName: $('[name="userName"]').val() },

    });

    res = JSON.parse(res);

    console.log(res);

    if (res.code === 1) {
        $('[name="nameSpan"]').html(`<span style="color:blue">${res.msg}</span>`);

    } else if (res.code === 0) {
        $('[name="nameSpan"]').html(`<b style="color:red">${res.msg}</b>`);

    }
}

async function register() {

    let userName = $('[name="userName"]').val();
    let pwd1 = $('[name="userPwd1"]').val();



    let res = await myPromiseAjax({
        url: '../server/goods_register.php',
        type: 'post',
        data: {
            userName: userName,
            userPwd: pwd1,
        },

    });

    res = JSON.parse(res);

    console.log(res);

    if (res.code === 0) {
        $('h2').html('<span style="color:red">您注册失败</span>');

        $('#sc').html(setVC({ bool: false }));

    } else if (res.code === 1) {
        let num = 5;
        $('h2').html(`<span style="color:#adc9ff">注册成功 ${num}秒之后 跳转 首页面 点击 跳转 <a href="./login.html?url=123>登录页</a> </span>`)                                     

        let time = setInterval(() => {
            num--;
            $('h2').html(`<span style="color:#adc9ff">注册成功 ${num}秒之后 跳转 首页面 点击 跳转 <a href="./login.html?url=123">登录页</a> </span>`)

            if (num === 1) {
                clearInterval(time);
                window.location.href = './index.html';
            }
        }, 1000);
    }
}


``